<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="(item,index) in dynamicMenu">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="index" class="text">
                        <template slot="title">
                            <!-- :class="{'active-text-color': isActive(item.index)}" -->
                                <i :class="item.icon"></i><span slot="title" >{{ item.title }}</span>
                        </template>
                        <template v-for="(subItem,subIndex) in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subIndex">
                                <template slot="title"> <i :class="subItem.icon"></i>{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem,i) in subItem.subs" :key="i" :index="threeItem.index">
                                    <i :class="subItem.icon"></i>{{ threeItem.title }}
                                </el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subIndex+1">
                                <i :class="subItem.icon"></i>{{ subItem.title }}
                            </el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.index" :key="index">
                        <i :class="item.icon"></i><span slot="title">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from './bus';
import loader from '../../utils/Loader';
export default {
        data() {
            return {
                collapse: false,
                menus:[]
            }
        },
        props:{
          items:Array
        },
        computed:{
            onRoutes(){
              return this.$route.path;
            },
            dynamicMenu(){
                const dynamicMenu = this.menus.length>0?this.menus:this.items;
                return dynamicMenu;
            }
        },
        created(){
            // 通过 Event Bus 进行组件间通信，来折叠侧边栏
            bus.$on('collapse', msg => {
                this.collapse = msg;
            })
            bus.$on('menus', val => {
                const menu = loader.loadMenusByTopIndex(val);
                this.menus = menu;
            })
        }
    }
</script>

<style scoped>
    .sidebar{
        display: block;
        position: absolute;
        left: 0;
        top: 50px;
        bottom:0;
        overflow-y: scroll;
    }
    .sidebar::-webkit-scrollbar{
        width: 0;
    }
    .sidebar-el-menu:not(.el-menu--collapse){
        width: 250px;
    }
    .sidebar > ul {
        height:100%;
    }
    .el-submenu > ul > li{
        background-color:#1f2d3d!important;
    }
    .el-submenu > ul > li:hover {
        background-color: #001528!important;
    }
</style>
